package com.example.pingxue.controller;

import com.example.pingxue.entity.Student;
import com.example.pingxue.entity.TClass;
import com.example.pingxue.service.ClassService;
import com.example.pingxue.service.ScoreService;
import com.example.pingxue.service.StudentService;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping
public class MainController {
    private final StudentService studentService;
    private final ClassService classService;
    private final ScoreService scoreService;

    public MainController(StudentService studentService, ClassService classService, ScoreService scoreService) {
        this.studentService = studentService;
        this.classService = classService;
        this.scoreService = scoreService;
    }

    // 学生选课
    @PostMapping("/students/{studentId}/classes/{classId}/select")
    public ResponseEntity<String> selectClass(@PathVariable Long studentId, @PathVariable Long classId) {
        try {
            studentService.selectClass(studentId, classId);
            return ResponseEntity.ok("选课成功");
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    // 学生退课
    @PostMapping("/students/{studentId}/classes/{classId}/drop")
    public ResponseEntity<String> dropClass(@PathVariable Long studentId, @PathVariable Long classId) {
        try {
            studentService.dropClass(studentId, classId);
            return ResponseEntity.ok("退课成功");
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    // 学生查看课程表
    @GetMapping("/students/{studentId}/schedule")
    public ResponseEntity<Set<TClass>> getCourseSchedule(@PathVariable Long studentId) {
        try {
            Set<TClass> schedule = studentService.getCourseSchedule(studentId);
            return ResponseEntity.ok(schedule);
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 查看班级花名册
    @GetMapping("/classes/{classId}/roster")
    public ResponseEntity<Set<Student>> getClassRoster(@PathVariable Long classId) {
        try {
            Set<Student> roster = classService.getRoster(classId);
            return ResponseEntity.ok(roster);
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 给教学班分配教师
    @PostMapping("/classes/{classId}/teachers/{teacherId}/assign")
    public ResponseEntity<String> assignTeacherToClass(@PathVariable Long classId, @PathVariable Long teacherId) {
        try {
            classService.assignTeacher(classId, teacherId);
            return ResponseEntity.ok("教师分配成功");
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 给教学班设置课程
    @PostMapping("/classes/{classId}/courses/{courseId}/set")
    public ResponseEntity<String> setCourseForClass(@PathVariable Long classId, @PathVariable Long courseId) {
        try {
            classService.setCourse(classId, courseId);
            return ResponseEntity.ok("课程设置成功");
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 教师给学生打分
    @PostMapping("/teachers/{teacherId}/selections/{selectionId}/score")
    public ResponseEntity<String> scoreStudent(
            @PathVariable Long teacherId,
            @PathVariable Long selectionId,
            @RequestParam(required = false) Double midScore,
            @RequestParam(required = false) Double finalScore,
            @RequestParam(required = false) Double performanceScore) {
        try {
            scoreService.scoreStudent(teacherId, selectionId, midScore, finalScore, performanceScore);
            return ResponseEntity.ok("打分成功");
        } catch (SecurityException e) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getMessage());
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }
}
